package com.lucy.flow.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.flow.domain.entity.FlowDefine;
import com.lucy.flow.params.FlowDefineCreateParams;
import com.lucy.flow.params.FlowDefineQueryParams;
import com.lucy.flow.service.FlowDefineService;
import com.lucy.flow.utils.XmlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Optional;

@RestController
@RequestMapping("flow")
@Api(tags = "流程定义管理")
public class FlowDefineController {
    @Autowired
    FlowDefineService service;

    /**
     * 流程定义管理
     * @param params 流程定义新建参数
     * @return 流程定义实体
     */
    @PostMapping(value = "define")
    @ApiOperation(value = "流程定义新增",notes = "")
    public ResponseDTO<Object> flowDefineCreate(@RequestBody @Validated FlowDefineCreateParams params){
        Optional<FlowDefine> existOp = service.getByDefineCode(params.getFlowDefineCode());
        if (existOp.isPresent()){
            return ResponseDTO.wrap(HttpStatus.BAD_REQUEST,"存在重复流程定义");
        }
        FlowDefine define = new FlowDefine();
        BeanUtil.copyProperties(params,define);
        return ResponseDTO.succData(
                service.saveOne(define)
        );
    }

    @DeleteMapping("define/{defineId}")
    @ApiOperation(value = "流程定义删除",notes = "传入流程定义ID,删除流程定义信息")
    public ResponseDTO<Object> flowDefineDelete(@PathVariable("defineId") String defineId){
        service.deleteOne(defineId);
        return ResponseDTO.succMsg("删除成功");
    }

    @ApiIgnore
    @PutMapping("define")
    @ApiOperation(value = "流程定义更新",notes = "")
    public ResponseDTO<Object> flowDefineUpdate(){
        return null;
    }

    @GetMapping("define")
    @ApiOperation(value = "流程定义列表",notes = "")
    public ResponseDTO<Object> flowDefineListQuery(FlowDefineQueryParams params){
        Sort sort;
        if(params.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);

        FlowDefine define = new FlowDefine();
        BeanUtil.copyProperties(params,define, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<FlowDefine> example = Example.of(define);

        return ResponseDTO.succData(service.getAll(pageable,example));
    }

    @GetMapping("define/{defineId}")
    @ApiOperation(value = "流程定义查询")
    public ResponseDTO<Object> flowDefineQuery(@PathVariable("defineId") String defineId){
        Optional<FlowDefine> defineOp = service.getOne(defineId);
        return defineOp.<ResponseDTO<Object>>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(HttpStatus.NOT_FOUND));
    }

}
